package com.skyhub.service.impl;

import com.skyhub.common.domain.dto.UploadFormDto;
import com.skyhub.common.domain.dto.UserDTO;
import com.skyhub.common.domain.pojo.ArticleSeries;
import com.skyhub.common.domain.pojo.User;
import com.skyhub.common.domain.vo.UserLoginVo;
import com.skyhub.common.exception.EmailLoginIllegalException;
import com.skyhub.common.utils.mail.MailUtils;
import com.skyhub.common.utils.userContext.UserContext;
import com.skyhub.common.utils.userToken.TokenUtils;
import com.skyhub.mapper.UploadMapper;
import com.skyhub.mapper.UserMapper;
import com.skyhub.service.UserService;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final UserContext userContext;

    private final TokenUtils utils;

    private final JavaMailSender javaMailSender;

    private final Jedis jedis;

    @Override
    public UserLoginVo loginForUsername(User user) {
        Map<String,Object>map=new HashMap<>();

        map.put("username",user.getUsername());

        map.put("password",user.getPassword());

        String token=utils.GenerateToken(map);

        User u=userMapper.loginForUsername(user);

        jedis.setex("skyHub:token:"+u.getUsername(),24*60*60,token);//24小时内有效

        userContext.setUser(u);//设置用户信息,ThreadLocal
        return new UserLoginVo(u,token);
    }

    @Override
    public UserLoginVo loginForEmail(User user) {
        Map<String,Object>map=new HashMap<>();

        map.put("email",user.getEmail());

        map.put("password",user.getPassword());

        String token=utils.GenerateToken(map);

        User u=userMapper.loginForUsername(user);

        jedis.setex("skyHub:token:"+u.getUsername(),24*60*60,token);//24小时内有效

        userContext.setUser(u);//设置用户信息,ThreadLocal

        return new UserLoginVo(u,token);
    }
    @Override
    public String getRegisterValidiCode(User user) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom("13929944553@163.com");
            try {
                helper.setSubject("注册验证码");
            } catch (EmailLoginIllegalException e) {
                throw new EmailLoginIllegalException("邮箱非法,我们目前仅支持163邮箱");
            }
            helper.setTo(user.getEmail());
            helper.setSubject("天枢笔迹-邮箱验证码");
            String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
            String html=MailUtils.GenerateHTML(code);
            helper.setText(html,true);
            javaMailSender.send(message);
            Thread.sleep(1500);
            jedis.setex("skyHub:validCode:"+user.getEmail(),100,code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "发送成功";
    }
    @Override
    public Boolean Register(UserDTO userDTO) {
        if(!userDTO.getValidCode().equals(jedis.get("skyHub:validCode:"+userDTO.getEmail()))){
            return false;
        }else{
            userMapper.registerInputData(userDTO);
            return true;
        }
    }
    @Override
    public Boolean ForgetPassword(UserDTO userDTO) {
        String code=jedis.get("skyHub:validCode:"+userDTO.getEmail());
        if(userDTO.getValidCode().equals(code)){
            userMapper.updatePassword(userDTO);
            return true;
        }else{
            return false;
        }
    }


}
